package com.inspur.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;



import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.pojo.RegularMeet;
import com.inspur.pojo.RegularMeetExt;
import com.inspur.pojo.user;
import com.inspur.service.RegularMeetService;

@Controller
public class RegularMeetController {
	@Autowired
	private RegularMeetService regularMeetService;

	@RequestMapping(value = "/regularMeeting")
	public String getRegularMeetList(Model model,
			@RequestParam(value = "pn", defaultValue = "1") Integer pn,
			HttpServletRequest request) {
		String mgrId=request.getParameter("mgrId");
		String meetDate=request.getParameter("meetDate");
		String theme=request.getParameter("theme");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("mgrId", mgrId);
		map.put("meetDate", meetDate);
		map.put("theme", theme);
		PageHelper.startPage(pn, 3);
		List<RegularMeetExt> meetList = null;
	
		// 调用Service层

		meetList = regularMeetService.getMeetList(map);
	
		PageInfo<RegularMeetExt> pageInfo = new PageInfo<RegularMeetExt>(meetList,
				meetList.size());

		System.out.println("例会的条数：" + meetList.size());
		model.addAttribute("pageInfo", pageInfo);

		return "/manager/regularMeeting.jsp";
	}

	@RequestMapping("/regularMeetingAdd")
	public String regularMeetAdd(RegularMeet regularMeet, Model model,
			MultipartFile upload, HttpServletRequest req) {
		String fileName = "";
		try {// 原始⽂件名称
			if (upload != null) {
				fileName = upload.getOriginalFilename();
				// 上传图⽚物理路径
				String url = req.getSession().getServletContext()
						.getRealPath("/upload/meet");
				File uploadfile = new java.io.File(url + "/" + fileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);
			}

		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		regularMeet.setAttachment(fileName);
		regularMeetService.addRegularMeeting(regularMeet);
		model.addAttribute("saveFlag", "1");
		model.addAttribute("meet", regularMeet);
		model.addAttribute("message", "保存成功");
		return "/manager/regularMeetingAdd.jsp";
	}

	@RequestMapping("/downLoadFile")
	public ResponseEntity<byte[]> downFile(String fileName,
			HttpServletRequest req) throws IOException {
		String url = req.getSession().getServletContext()
				.getRealPath("/upload/meet");
		File file = new File(url + "/" + fileName);
		HttpHeaders headers = new HttpHeaders();
		// 下载显示的⽂件名，解决中⽂名称乱码问题
		String downloadFielName = new String(fileName.getBytes("utf-8"),"iso-8859-1");   //iso-8859-1 ,"utf-8"
		// 通知浏览器以attachment（下载⽅式）打开图⽚
		headers.setContentDispositionFormData("attachment", downloadFielName);
		// application/octet-stream：⼆进制流数据（最常⻅的⽂件下载）。
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.CREATED);

	}
	
	@RequestMapping("/regularMeetingUpdate")
	 public String regularMeetingUpdate(RegularMeet regularMeet,Model model,MultipartFile upload, HttpServletRequest req){
		String fileName = "";
		try {// 原始⽂件名称
			if (upload != null) {
				fileName = upload.getOriginalFilename();
				// 上传图⽚物理路径
				String url = req.getSession().getServletContext()
						.getRealPath("/upload/meet");
				File uploadfile = new java.io.File(url + "/" + fileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);
				regularMeet.setAttachment(fileName);
			}

		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		regularMeetService.regularMeetingUpdate(regularMeet);
		model.addAttribute("saveFlag", "1");
		model.addAttribute("meet", regularMeet);
		model.addAttribute("message", "修改成功");
		return "/manager/regularMeetingUpdate.jsp";
	}
	@RequestMapping("/modifymeeting")
	public String modifymeeting(String cmkey,Model model){
	  int meet_key = Integer.parseInt(cmkey);
	  RegularMeet meet = regularMeetService.getMeetbycmkey(meet_key);
	  model.addAttribute("meet", meet);
	  return "/manager/regularMeetingUpdate.jsp";
	}
	
	@RequestMapping("/deletemeet")
	public String deletemeet(String cmkeys){
		String cmkey[] = cmkeys.split(",");
		int meet_cmkeys[] = new int[cmkey.length];
		for(int i=0;i<cmkey.length;i++){
			meet_cmkeys[i] = Integer.parseInt(cmkey[i]);
		}
		regularMeetService.deletemeet(meet_cmkeys);
		
		//删除数据后要重新查询一次
		return "redirect:regularMeeting";
		
	}
}
